package com.hsylient.javase.chapter10;

import org.junit.Test;

import java.util.Comparator;
import java.util.Objects;

public class MyObjects {
    /**
     * 与传统的方式相比，避免了控制真的问题
     */
    @Test
    public void testMethodEquals() {
        String str = null;
        boolean result = Objects.equals(str, "aaa");
        System.out.println(result);
    }

    /**
     * 与传统的方式相比，避免了控制真的问题
     */
    @Test
    public void testEquals() {
        System.out.println(Objects.equals("xxxx", "yyyy"));
    }

    /**
     * 底层调用了 Arrays.deepEquals(Object[], Object[]),
     */
    @Test
    public void testDeepEquals() {
        int[] integer = {1, 2, 3, 4, 5};
        int[] otherInteger = {1, 2, 3, 4, 5};
        System.out.println( Objects.deepEquals(integer, otherInteger));
    }


    @Test
    public void testHashCode() {
        System.out.println(Objects.hashCode(new Object()));
    }

    @Test
    public void testToString() {
        System.out.println(Objects.toString(new Object()));
        System.out.println(Objects.toString(null, "if null return default value"));
    }

    @Test
    public void testCompare() {
        System.out.println( Objects.compare(1, 2, Comparator.comparingInt(x -> x)));
    }

    @Test
    public void testRequireNull() {
        String s = null;
        System.out.println(Objects.requireNonNull(s));
        System.out.println(Objects.requireNonNull(s, "s is null"));
        System.out.println(Objects.requireNonNull(s, MyObjects::supplier));
    }

    @Test
    public void testIsNull() {
        System.out.println(Objects.isNull(null));
        System.out.println(Objects.nonNull(null));
    }

    @Test
    public void testRequireNonNullElse() {
        System.out.println(Objects.requireNonNullElse(null, "default value"));
    }

    @Test
    public void testRequireNonNullElseGet() {
        System.out.println(Objects.requireNonNullElseGet(null, MyObjects::supplier));
    }

    @Test
    public void testRequireNonNull() {
        Object o = null;
        System.out.println(Objects.requireNonNull(o, MyObjects::supplier));
    }

    @Test
    public void testCheckIndex() {
        System.out.println(Objects.checkIndex(-1, 12));
        System.out.println(Objects.checkIndex(3, 2));
        System.out.println(Objects.checkIndex(12, -2));
    }

    @Test
    public void testCheckFromToIndex() {
        System.out.println(Objects.checkFromToIndex(1, 5, 2));
    }

    @Test
    public void testCheckFromIndexSize() {
        System.out.println(Objects.checkFromIndexSize(0, 3, 4));
    }

    private static String supplier() {
        return "快乐小少年";
    }

}
